package com.hc.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.common.service.RedisService;
import com.hc.constant.RedisConst;
import com.hc.domain.Category;
import com.hc.res.CategoryItem;
import com.hc.service.CategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hecai
 */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @PostMapping("/v1")
    public Result create(@RequestBody Category category) {
        boolean res = categoryService.save(category);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping("/v1")
    public Result updateById1(@RequestBody Category category) {
        boolean res = categoryService.updateById(category);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("/v1/{id}")
    public Result removeById1(@PathVariable Serializable id) {
        boolean res = categoryService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("/v1/{id}")
    public Result getById1(@PathVariable Serializable id) {
        Category res = categoryService.getById(id);

        return ResultUtil.success("查询成功").setData(res);
    }

    @Resource
    private RedisService<List<CategoryItem>> redisServiceList;

    /**
     * 首页商品类别
     *
     * @return
     */
    @ApiOperation(value = "首页商品类别", notes = "", httpMethod = "GET")
    @GetMapping("/v1/indexCategories")
    public Result getTopCategory() {
        List CategoryList = redisServiceList.get(RedisConst.CATEGORY + "::tree");

        return ResultUtil.success().setData(CategoryList);
    }

}